﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;

namespace ZFB.WebAPI.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserServices _userServices;

        public UserController(IUserServices userServices)
        {
            _userServices = userServices;
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="addUserDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddNew(AddUserDTO addUserDTO) 
        {
            return await _userServices.AddNewAsync(addUserDTO.phoneNum, addUserDTO.password);
        }
        /// <summary>
        /// 根据id获取用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetById(long id) 
        {
            return await _userServices.GetById(id);
        }
        /// <summary>
        /// 根据手机号获取
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetByPhoneNum(string phoneNum) 
        {
            return await _userServices.GetByPhoneNumAsync(phoneNum);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> CheckLogin(LoginDTO loginDTO) 
        {
            return await _userServices.CheckLoginAsync(loginDTO.phoneNum, loginDTO.password);
        }
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <param name="newPassword"></param>
        [HttpPut]
        public async Task<ApiResult> UpdatePwd(string phoneNum, string newPassword) 
        {
            return await _userServices.UpdatePwdAsync(phoneNum, newPassword);
        }
        /// <summary>
        /// 设置用户userId 的城市id
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="cityId"></param>
        [HttpPut]
        public async Task<ApiResult> SetUserCityId(long userId, long cityId) 
        {
            return await _userServices.SetUserCityIdAsync(userId, cityId); 
        }
    }
}
